'use strict';

const Controller = require('egg').Controller

class WebpageController extends Controller {
  async list() {
    const { ctx, service } = this;
    service.webpage.find().then(res => {
      ctx.body = {
        code: 200,
        message: '查询成功',
        data: res,
      };
    }).catch(err => {
      ctx.body = {
        code: 400,
        message: err,
      };
    });
  }

  async addWebPage() {
    const { ctx, service } = this;
    const query = ctx.request.body;
    await service.webpage.add(query).then(res => {
      ctx.body = {
        code: 200,
        message: '添加成功',
      };
    }).catch(err => {
      ctx.body = {
        code: 400,
        message: err,
      };
    });
  }

  async getWebPage() {
    const { ctx, service } = this;
    const cat_id = ctx.params.cat_id;
    const wid = ctx.params.wid;
    await service.webpage.show(cat_id, wid).then(res => {
      ctx.body = {
        code: 200,
        data: res,
        message: '查询成功',
      };
    }).catch(err => {
      ctx.body = {
        code: 400,
        message: err,
      };
    });
  }

  async editWebPage() {
    const { ctx, service } = this;
    const query = ctx.request.body;
    await service.webpage.edit(query).then(res => {
      ctx.body = {
        code: 200,
        message: '编辑成功',
      };
    }).catch(err => {
      ctx.body = {
        code: 400,
        message: err,
      };
    });
  }

  async delWebPage() {
    const { ctx, service } = this;
    const wid = ctx.request.body.wid;
    await service.webpage.delete(wid).then(res => {
      ctx.body = {
        code: 200,
        message: '编辑成功',
      };
    }).catch(err => {
      ctx.body = {
        code: 400,
        message: err,
      };
    });
  }
}

module.exports = WebpageController;
